package com.zhengsr.latte.ec.ec.main.recyclerview;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;

import com.zhengsr.latte.ui.recycler.adapter.RMultiAdapter;
import com.zhengsr.latte.ui.recycler.adapter.base.RBaseViewholder;
import com.zhengsr.latte.ec.R;
import com.zhengsr.latte.ui.glide.GlideManager;
import com.zhengsr.latte.ui.recycler.ItemType;
import com.zhengsr.latte.ui.recycler.MultipleBean;
import com.zhengsr.latte.ui.recycler.MultipleConfig;
import com.zhengsr.viewpagerlib.bean.PageBean;
import com.zhengsr.viewpagerlib.callback.PageHelperListener;
import com.zhengsr.viewpagerlib.view.BannerViewPager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhengshaorui on 2018/5/27.
 */

public class MultiAdapter extends RMultiAdapter<MultipleBean> {
    private Context mContext;
    public MultiAdapter(Context context, List<MultipleBean> beans) {
        super(context, beans);
        mContext = context;
        addItemType(ItemType.TYPE_IMAGE_TEXT, R.layout.item_multiple_image_text);
        addItemType(ItemType.TYPE_IMAGE,R.layout.item_multiple_image);
        addItemType(ItemType.TYPE_TEXT,R.layout.item_multiple_text);
        addItemType(ItemType.TYPE_BANNER,R.layout.item_multiple_banner);
    }

    @Override
    protected void getConver(RBaseViewholder holder, MultipleBean data, int position) {
        int viewType = getItemViewType(position);
        switch (viewType){
            case ItemType.TYPE_IMAGE_TEXT:
                new GlideManager.Builder()
                        .setContext(mContext)
                        .setTargetSource((String) data.getItemField(MultipleConfig.IMAGE_URL))
                        .setBitmapScanType(GlideManager.BITMAP_SCAN_CENTERN)
                        .setImageView((ImageView) holder.getItemView(R.id.item_multi_it_iv))
                        .builder();

                holder.setText(R.id.item_multi_it_tv,
                        (String) data.getItemField(MultipleConfig.TEXT));
                break;
            case ItemType.TYPE_IMAGE:

                new GlideManager.Builder()
                        .setContext(mContext)
                        .setTargetSource((String) data.getItemField(MultipleConfig.IMAGE_URL))
                        .setBitmapScanType(GlideManager.BITMAP_SCAN_CENTERN)
                        .setImageView((ImageView) holder.getItemView(R.id.item_multi_image))
                        .builder();

                break;
            case ItemType.TYPE_TEXT:
                holder.setText(R.id.item_mulit_text,
                        (String) data.getItemField(MultipleConfig.TEXT));
                break;
            case ItemType.TYPE_BANNER:
                List<String> lists = new ArrayList<>();
                lists.addAll((List<String>)data.getItemField(MultipleConfig.BANNERS));

                PageBean pageBean = new PageBean.Builder<String>()
                        .setDataObjects(lists)
                        .builder();
                BannerViewPager bannerViewPager = holder.getItemView(R.id.item_multi_banner);
                bannerViewPager.setPageListener(pageBean, R.layout.banner_item_layout, new PageHelperListener() {
                    @Override
                    public void getItemView(View view, Object o) {
                        ImageView imageView = (ImageView) view;
                        String url = (String) o;
                        new GlideManager.Builder()
                                .setContext(mContext)
                                .setTargetSource(url)
                                .setBitmapScanType(GlideManager.BITMAP_SCAN_CENTERN)
                                .setImageView(imageView)
                                .builder();

                        imageView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {

                            }
                        });
                    }
                });
                break;
            default:
                break;
        }
    }
}
